#!/bin/sh
# PCP QA Test No. 1889
# test primary pmlogger starts under systemd when service is disabled
#
# Copyright (c) 2021 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# this tests systemd starting pmlogger
[ "$PCPQA_SYSTEMD" = yes ] || _notrun "systemctl not installed or not active"

_cleanup()
{
    cd $here
    if [ -n "$pid" ]
    then
	$sudo rm -f $PCP_TMP_DIR/pmlogger/$pid $PCP_RUN_DIR/pmlogger.$pid.socket
    fi
    $sudo rm -f $PCP_RUN_DIR/pmlogger.pid
    echo "=== enable pmlogger.service"
    $sudo systemctl enable pmlogger.service >/dev/null 2>&1
    _service pmlogger restart | _filter_pcp_restart
    _wait_for_pmlogger
    _restore_auto_restart pmlogger
    rm -rf $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

# real QA test starts here
_stop_auto_restart pmlogger

pid=`$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger.*-P' | \
grep -v grep | awk '{print $2}'`

if [ ! -S "$PCP_RUN_DIR/pmlogger.$pid.socket" ]; then
    echo FAIL no primary pmlogger running? pid=$pid
    status=1
    exit
fi
echo found primary pmlogger and found control socket in PCP_RUN_DIR

echo "=== disable pmlogger.service" | tee -a $seq_full
$sudo systemctl disable pmlogger.service >>$seq_full 2>&1

echo === start primary logger whilst disabled ===
if ! _service pmlogger start; then _exit 1; fi \
| _filter_pcp_start
_wait_for_pmlogger || _exit 1

newpid=`$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '/[p]mlogger.*-P' | \
    grep -v grep | awk '{print $2}'`

if [ -z "$newpid" ]; then
    echo FAIL pmlogger failed to start whilst disabled
    status=1
    exit
fi
echo found primary pmlogger and found control socket in PCP_RUN_DIR
status=0

exit
